package com.air.thread;

/**
 * @author shent
 */
public class ThreadLocalDemo {
    public static class MyRunnable implements Runnable {
        private ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

        @Override
        public void run() {
            threadLocal.set((int) (Math.random() * 100D));
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread().getName()+" number: "+threadLocal.get());
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyRunnable sharedRunnableInstance = new MyRunnable();
        Thread t1 = new Thread(sharedRunnableInstance,"t1");
        Thread t2 = new Thread(sharedRunnableInstance,"t2");
        t1.start();
        t2.start();
        t1.join();//main thread wait for thread 1 to terminate
        t2.join();//main thread wait for thread 2 to terminate
    }
}
